<template>
	<div class="sys-pos-container">
		<el-dialog v-model="state.isShowDialog" draggable :close-on-click-modal="false" width="700px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit />
					</el-icon>
					<span> {{ props.title }} </span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" class="fl">
				<el-form-item label="区域名称" prop="regionName"
					:rules="[{ required: true, message: '区域名称不能为空', trigger: 'blur' }]">
					<el-input v-model="state.ruleForm.regionName" placeholder="名称" clearable class="input" />
				</el-form-item>
				<el-form-item label="所属仓库" prop="whNum"
					:rules="[{ required: true, message: '所属仓库不能为空', trigger: 'blur' }]">
					<el-select v-model="state.ruleForm.whNum" filterable clearable class="w100">
						<el-option v-for="item in state.wareHouseNames" :key="item"
							:label="item.eg_wms_warehouse.WHName" :value="item.eg_wms_warehouse.WHNum" />
					</el-select>
				</el-form-item>
				<el-form-item label="绑定物料">
					<el-select v-model="state.ruleForm.regionMaterielNum" filterable clearable class="w100">
						<el-option v-for="item in state.materialList" :key="item.materielNum" :label="item.materielName"
							:value="item.materielNum" />
					</el-select>
				</el-form-item>
				<el-form-item label="状态">
					<el-switch v-model="state.ruleForm.regionStatus" inline-prompt active-text="启用" inactive-text="禁用"
						:active-value="0" :inactive-value="1" />
				</el-form-item>
				<el-form-item label="备注">
					<el-input v-model="state.ruleForm.regionRemake" placeholder="请输入备注内容" clearable type="textarea"
						class="extra" />
				</el-form-item>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="sysEditPos">
import { reactive, ref } from 'vue';

import { getAPI } from '/@/utils/axios-utils';
import { APIJSONApi, SysDictDataApi, SysPosApi } from '/@/api-services/api';
import { WmsApi } from '/@/api/wh';
import { ElMessage } from 'element-plus';
import { feature } from "/@/utils/axios-utils";

const props = defineProps({
	title: String,
});
const emits = defineEmits(['handleQuery']);
const ruleFormRef = ref();
const state = reactive({
	isShowDialog: false,
	ruleForm: {

	},
	materialList:[],
	wareHouseNames:[]
});

// 打开弹窗
const openDialog = async (row: any) => {
	try {
	const res = await feature(
        getAPI(APIJSONApi).apiAPIJSONGetPost({
          "[]": {
            "eg_wms_warehouse": {
              IsDelete: 0,
              "@column": "WHName,WHNum",
            },
          },
        })
      );
  console.log(res);
  
      state.wareHouseNames = res[1].data.result["[]"];
    } catch (error) {
      console.log(error);
    }
	await WmsApi()
      .MaterielPagePost({
        page: 0,
        pageSize: 100,
      })
      .then(function (res) {
        state.materialList = res.data.result.items;
        console.log(state.materialList);
      })
      .catch(function (error) {
        console.log(error);
      });
	state.ruleForm = JSON.parse(JSON.stringify(row));
	console.log(state.ruleForm.regionStatus);
	

  
	state.isShowDialog = true;
	ruleFormRef.value?.resetFields();
};

// 关闭弹窗
const closeDialog = () => {
	emits('handleQuery');
	state.isShowDialog = false;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

// 提交
const submit = () => {
	ruleFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
		if (state.ruleForm.id) {
			console.log(state.ruleForm);

			await WmsApi().RegionPagePut(state.ruleForm)
				.then(function (res) {
					ElMessage.success('修改成功');
					closeDialog();
				})
				.catch(function (error) {
					console.log(error);
				});
		} else {

			await WmsApi().RegionPageAdd(state.ruleForm)
				.then(function (res) {
					ElMessage.success('新增成功');

					closeDialog();
				})
				.catch(function (error) {
					console.log(error);
				});

		}
	});
}

// 导出对象
defineExpose({ openDialog });
</script>
<style lang="scss" scoped>
.fl {
	width: 70%;
	margin: 20px auto;
}
</style>